﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// Tầng này chỉ giao tiếp với BusinessLogic
using qlHieuThuocDongY.BusinessLogic;
using qlHieuThuocDongY.BusinessObject;

namespace qlHieuThuocDongY
{
    public partial class frmThongTinhBenhNhan : Form
    {
        thongTinBenhNhanBUS bnBUS = new thongTinBenhNhanBUS();
        public frmThongTinhBenhNhan()
        {
            InitializeComponent();
        }

        private void frmThongTinhBenhNhan_Load(object sender, EventArgs e)
        {
            
          
            // Load ComBox Giới Tính
            cboGioiTinh.DataSource = bnBUS.LayDanhSachGioiTinh();
            cboGioiTinh.DisplayMember = "NameGT";
            cboGioiTinh.ValueMember = "GioiTinh";
            // Load ComBox giới tính trong dataGridViewThongTinhBenhNhan
            colGioiTinh.DataSource = bnBUS.LayDanhSachGioiTinh();
            colGioiTinh.DisplayMember = "NameGT";
            colGioiTinh.ValueMember = "GioiTinh";
            // DataGridView phải được load sau cùng
            // Load dataGridViewBenhNhan
            dataGridViewBenhNhan.DataSource = bnBUS.LayDanhSachBenhNhan();
            EnableEditing(false);
        }

        private void dataGridViewBenhNhan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // dòng hiện tại
            int dong = e.RowIndex;
            // thực thi binding data lên control, khi click vào row dataGridView
            txtMaBN.Text = dataGridViewBenhNhan.Rows[dong].Cells["MaBN"].Value.ToString();
            txtTenBN.Text = dataGridViewBenhNhan.Rows[dong].Cells["TenBN"].Value.ToString();
            dtPickerNgaySinh.Text = dataGridViewBenhNhan.Rows[dong].Cells["NgaySinh"].Value.ToString();
            cboGioiTinh.SelectedValue = dataGridViewBenhNhan.Rows[dong].Cells["colGioiTinh"].Value.ToString();
            txtDiaChi.Text = dataGridViewBenhNhan.Rows[dong].Cells["DiaChi"].Value.ToString();
            txtDienThoai.Text = dataGridViewBenhNhan.Rows[dong].Cells["DienThoai"].Value.ToString();
        }

        private void EnableEditing(bool editing)
        {
            //button
            btnThem.Enabled = !editing;
            btnSua.Enabled = !editing;
            btnXoa.Enabled = !editing;
            btnLuu.Enabled = editing;
            btnKhongLuu.Enabled = editing;
            // texBox,Combobox
            txtTenBN.Enabled = editing;
            txtDiaChi.Enabled = editing;
            txtDienThoai.Enabled = editing;
            dtPickerNgaySinh.Enabled = editing;
            cboGioiTinh.Enabled = editing;
            // DataGridView
            dataGridViewBenhNhan.Enabled = !editing;
        }

        private void ResetTextValue()
        {
            txtTenBN.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
            ResetTextValue();
           // BenhNhan bn = new BenhNhan();
            // Load NextID lên texbox MaBN
            txtMaBN.Text = bnBUS.NextID();
        }

        private void btnKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }
        private BenhNhan LayTTBN()
        {
            BenhNhan bn = new BenhNhan();
            bn.MaBN = txtMaBN.Text;
            bn.TenBN = txtTenBN.Text;
            // chuyển đổi sang kiểu dử liệu DateTime
            bn.NgaySinh = Convert.ToDateTime(dtPickerNgaySinh.Value.ToShortDateString());
            bn.GioiTinh = (cboGioiTinh.SelectedValue.ToString() == "True") ? 1 : 0;
            bn.DiaChi = txtDiaChi.Text;
            bn.DienThoai = txtDienThoai.Text;
            return bn;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {

            BenhNhan bn = LayTTBN();
            // Nếu tồn tại mã bệnh nhân rồi thì là sửa
            if (bnBUS.CheckExists(bn.MaBN))
            {
                if (bnBUS.SuaBN(bn))
                    frmThongTinhBenhNhan_Load(sender, e);

            }
            else // Thêm
            {
                  if (bnBUS.ThemBN(bn))
                      frmThongTinhBenhNhan_Load(sender, e);
            }

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Bệnh Nhân: " + txtTenBN.Text + "Không?", "Hỏi", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)  
                if (bnBUS.XoaBN(txtMaBN.Text))
                    // load lại form
                     frmThongTinhBenhNhan_Load(sender,e);

        }
    }
}
